%% using grid to find the best value for C (box constraints) and gamma (scale)

cv = 5;

% big grid 
xmin = -15  ; xmax = 5  ; dx = 2;     % default (-15 : 2 : 5 )
ymin = -5   ; ymax = 15 ; dy = 2;     % default (-5  : 2 : 15)

% fine grid
% xmin = 1;xmax = 3; dx = 1/5;        % default (-15 : 2 : 5 )
% ymin = -6; ymax = -4; dy = 1/5;     % default (-5  : 2 : 15)

nx = (xmax - xmin)/dx +1;
ny = (ymax - ymin)/dy +1;
fres = zeros(nx,ny);

index1 = 1;
for i = xmin: dx: xmax
    index2 = 1;
    for  j = ymin: dy : ymax
        C = 2.^i;
        scale = 2.^j;
        params = sprintf('-s 3 -q -v %d -c %f -g %f',[cv, C, scale]);
        %since -v, then model return mean squared error
        rmse = svmtrain2(Yt_h, Xt, params);
        fres(index1,index2)= rmse;
        index2 = index2+1;
    end
    index1 = index1+1;    
end

minV = min(min(fres));

[row, col] = find(fres == minV);
z = 2.^[(dx*(row-1)+xmin), (dy*(col-1)+ymin)];
fprintf('the best cross validation is %f with (%d,%d)',[minV z]);

%draw the contour of value;
close all;
contour(xmin:dx:xmax, ymin:dy:ymax,fres','showtext','on')
